from sqlalchemy import (
    Column,
    Integer,
    Text,
    Unicode,
    Boolean,
    ForeignKey,
    )

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    relationship,
    backref,
    )

from ..models.meta import Base
from ..models.meta import DBSession as Session
from sqlalchemy import join


class ZSelectList(Base):
    __tablename__ = 'zSelectList'
    id = Column(Integer, primary_key=True)
    description = Column(Unicode(50), default=u'')
    name = Column(Unicode(50), default=u'')

class ZSelectListDetail(Base):
    __tablename__ = 'zSelectListDetail'
    id = Column(Integer, primary_key=True)
    text = Column(Unicode(50), default=u'')
    value = Column(Unicode(50), default=u'')
    listnameid = Column(Integer, ForeignKey('zSelectList.id')) 
    listname = relationship('ZSelectList', backref = backref('detail', order_by=id))


def getoptions_text(name):
    optiontext = []
    selectlist_q = Session.query(ZSelectList)
    selectlist = selectlist_q.filter(ZSelectList.name == name).all()
    if selectlist and len(selectlist) > 0:
        for detail_i in selectlist[0].detail:
            optiontext.append(detail_i.text)

    return optiontext

def getoptions_singletext(name, value):
    detail_q = Session.query(ZSelectListDetail)
    details = detail_q.select_from(ZSelectList).join(ZSelectList.detail).filter(ZSelectList.name == name).filter(ZSelectListDetail.value == str(value)).all()
    return details and details[0].text or ''

def getoptions(name, value=''):
    optiontext=''
    selectlist_q = Session.query(ZSelectList)
    selectlist = selectlist_q.filter(ZSelectList.name == name).all()
    if selectlist and len(selectlist) > 0:
        for detail_i in selectlist[0].detail:
            if detail_i.value==value:
                optiontext = optiontext + '<option value="%s" selected>%s</option>' % (detail_i.value,detail_i.text)
            else:
                optiontext = optiontext + '<option value="%s">%s</option>' % (detail_i.value,detail_i.text)
    return optiontext 


def getajaxoptions(name, ajaxfieldname_source, ajaxfieldname_target, value=''):
    #get the selectlist id
    selectlistname_id = None
    selectlistname_q = Session.query(ZSelectList)
    selectlistname = selectlistname_q.filter(ZSelectList.name == name).all()
    if selectlistname and len(selectlistname) > 0:
        selectlistname_id = selectlistname[0].id

        return """
        <script>
            $(document).ready(function(){
                var change_value = $("select[name='%s']").attr('value');
                $.get("/admin/selectList/ajaxsondetail",{selectlistid:'%s', fathervalue:change_value, value:'%s'},function(data) {
                    $("select[name='%s'] > option").remove();
                    $("select[name='%s']").html(data);
                });
                $("select[name='%s']").change(function(){
                    var change_value = $(this).attr('value');
                    $.get("/admin/selectList/ajaxsondetail",{selectlistid:'%s', fathervalue:change_value, value:'%s'},function(data) {
                            $("select[name='%s'] > option").remove();
                            $("select[name='%s']").html(data);
                    });
                })
            })
        </script>
        <!-- end -->
""" % (ajaxfieldname_source, selectlistname_id, value, ajaxfieldname_target, ajaxfieldname_target, ajaxfieldname_source, selectlistname_id, value, ajaxfieldname_target, ajaxfieldname_target)
